package com.haisen.controller;


import com.alibaba.fastjson.JSON;
import com.haisen.core.utils.PageResultSet;
import com.haisen.core.utils.Result;
import com.haisen.dataobject.entity.Customer;
import com.haisen.dataobject.query.CustomerQuery;
import com.haisen.service.CustomerService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/customer")
@Slf4j
public class CustomerController {

    @Autowired
    private CustomerService customerService;


    @GetMapping
    @RequiresPermissions("customer:view")
    public String page(Model model){
        log.info("访问到了此页面");
        //数据太多，一次添加到model慢
       // setCommonData(model);
        return "customer/customer";
    }

    @ResponseBody
    @RequestMapping("/list")
    public PageResultSet<Customer> list(CustomerQuery customerQuery){
        return customerService.findByPage(customerQuery);
    }

    @ResponseBody
    @PostMapping("/create")
    @RequiresPermissions("customer:create")
    public Result create( @Validated Customer customer){
        log.info("表单提交的form={}", JSON.toJSONString(customer));
      customerService.create(customer);
      return Result.success();
    }

    @ResponseBody
    @PostMapping("/update")
    @RequiresPermissions("customer:update")
    public Result update(@Validated Customer customer){
        customerService.update(customer);
        return Result.success();
    }

    @ResponseBody
    @PostMapping("/delete")
    @RequiresPermissions("customer:delete")
    public Result delete(@RequestParam("id") Long[] ids){
         customerService.delete(ids);
        return Result.success();
    }

    @ResponseBody
    @PostMapping("/import")
    @RequiresPermissions("customer:view")
    public Result importfile(@RequestParam("importpathfile") String pathfile){
        log.info("上传文件路径={}",pathfile);
        customerService.importExcel2DataBase(pathfile);
        return Result.success();
    }

    private void setCommonData(Model model) {
       model.addAttribute("customerList",customerService.findAll());
    }


}
